$.validator.setDefaults({
    debug: true
})
var baseUrl = "http://localhost:8080"

$(function () {

    // 自定义密码验证
    jQuery.validator.addMethod("passWord", function (value, element) {
        var passWord = /^(?=.*?[A-Za-z])(?=.*?[0-9])(?=.*((?=[\x21-\x7e]+)[^A-Za-z0-9])).{10,}$/;
        return this.optional(element) || (passWord.test(value));
    }, "请输入至少10位密码（至少包含1个字母，1个数字和1个特殊字符）");

    // 登录 注册 密码重置切换
    $('.logon_link').click(function () {
        goToLogon();
    });
    $('.login_link').click(function () {
        goToLogin();
    });
    $('.resetPwd_link').click(function () {
        goToRestPwd();
    });
    function goToLogin(){
        $('.containar').css('transform', 'translateX(80%)');
        $('.logon').addClass('nodisplay');
        $('.resetPwd').addClass('nodisplay');
        $('.login').find('input[name!=sex]').val('');
        $('.login').removeClass('nodisplay');
    }
    function goToLogon(){
        $('.containar').css('transform', 'translateX(80%)');
        $('.login').addClass('nodisplay');
        $('.resetPwd').addClass('nodisplay');
        $('.logon').find('input[name!=sex]').val('');
        $('.logon').removeClass('nodisplay');
    }
    function goToRestPwd(){
        $('.containar').css('transform', 'translateX(80%)');
        $('.login').addClass('nodisplay');
        $('.logon').addClass('nodisplay');
        $('.resetPwd').find('input').val('');
        $('.resetPwd').removeClass('nodisplay');
    }

    // 注册验证
    $('#logon_button').click(
        function () {
            var username = $('#username_on').val();
            var password = $('#password_on').val();
            var email = $("#email_on").val();
            var phone = $("#telephone_on").val();
            var birthday = $("#birthday_on").val();

            if (!checkPwd_inElem("password_on")) {
                return;
            }
            if (!checkPhone_inElem("telephone_on")) {
                return;
            }

            $.ajax({
                url: baseUrl + "/user/logonDo",
                data: JSON.stringify({
                    userId: username,
                    password: password,
                    email: email,
                    phone: phone,
                    birthday: birthday
                }),
                type: "post",
                contentType: "application/json;charset=UTF-8",
                dataType: "json",
                success: function (result) {
                    if(result) {
                        console.log(result);
                        alert(result.message);
                        if (result.status === 200) {

                            alert("注册成功!");
                            goToLogin();
                        } else {  // 登录失败
                            alert("注册失败!:"+result.message);
                        }
                    }else{
                        alert(result.message);
                    }
                },
                err: function (e) {
                    console.log(e,e.message);
                    alert("err");
                }
            });

            // alert("注册成功！");
        }
    )

    // 登录验证
    $('#login_button').click(function () {

        var username = $('#username_in').val();
        var password = $('#password_in').val();
        $.ajax({
            url: baseUrl + "/user/loginDo",
            data: JSON.stringify({
                userId: username,
                password: password
            }),
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success: function (result){
                if(result) {
                    console.log(result);
                    alert(result.message);
                    if (result.status === 200) {
                        setTimeout(function () {
                            window.location.href = baseUrl + "/main";
                        }, 1000)
                    } else {  // 登录失败
                        $('#username_in').val('');
                        $('#password_in').val('');
                    }
                }else{
                    alert(result.message);
                }
            },
            error: function (e){
                console.log(e,e.message);
                alert("err");
            }
        })
        if (!checkPwd_inElem("password_in")) {
            return;
        }
    });

    $("#login_form").submit(function () {
   		//写false，表示拦截
   		return false;
    });

    $("#logon_form").submit(function () {
   		//写false，表示拦截
   		return false;
    });

    $("#resetPwd_form").submit(function () {
        return false;
    });
    // 密码重置
    $('#resetPwd_button').click(function () {
        var pwd1_in = document.getElementById("password1_re");
        var pwd2_in = document.getElementById("password2_re");
        var username = document.getElementById("username_re").value;
        var phone = document.getElementById("telephone_re").value;
        var pwd1 = pwd1_in.value;
        var pwd2 = pwd2_in.value;

        if (pwd1 !== pwd2 || pwd1 == null || pwd2 == null) {
            pwd2_in.setCustomValidity("确认密码和新密码不一致！！");
            return;
        } else {
            pwd2_in.setCustomValidity("");
        }

        if (!checkPwd_inElem("password2_re")) {
            return;
        }

        $.ajax({
            url:baseUrl+"/user/updateDo",
            data: JSON.stringify({
                userId: username,
                password: pwd2,
                phone: phone
            }),
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            success:function (result) {
                if(result!=null){
                    alert(result.message);
                    if(result.status===200){
                        setTimeout(function (){
                            window.location.href=baseUrl+"/login";
                        },1000);
                    }
                }
                else{
                    alert("修改失败，请稍后重试");
                }
                // $('#username_re').val('');
                // $('#password1_re').val('');
                // $('#password2_re').val('');
                // $('#telephone_re').val('');
            },
            error:function(e){
                console.log(e,e.message);
                alert('error');
            }
        })
    });

    // 验证Elem_id对应输入的password
    function checkPwd_inElem(Elem_id) {
        var password_err = "密码长度不小于8位，包含“数字,大写字母,小写字母”中的至少三种~";
        var pwd_in = document.getElementById(Elem_id);
        var pwd = pwd_in.value;
        if (!checkPassword(pwd)) {
            pwd_in.setCustomValidity(password_err);
            return false;
        } else {
            pwd_in.setCustomValidity("");
            return true;
        }
    }

    // 验证Elem_id对应输入的手机号
    function checkPhone_inElem(Elem_id) {
        var phone_err = "请输入正确的手机号~";
        var phone_in = document.getElementById(Elem_id);
        var phone = phone_in.value;

        var regPhone = /1\d{10}/
        if (!regPhone.test(phone)) {
            phone_in.setCustomValidity(phone_err);
            return false;
        } else {
            phone_in.setCustomValidity("");
            return true;
        }
    }

    // 密码格式检查
    function checkPassword(pwd) {
        // 格式判断
        var regUpLetter = /[A-Z]/
        var regLowLetter = /[a-z]/
        var regNumber = /[0-9]/

        var complex = 0;
        if (regUpLetter.test(pwd)) {
            ++complex;
        }
        if (regLowLetter.test(pwd)) {
            ++complex;
        }
        if (regNumber.test(pwd)) {
            ++complex;
        }

        if (complex < 3 || pwd.length < 8) {
            return false;
        }

        return true;
    }
}
);

